﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MR_HKSUtility;
using MR_HKSCalc;
namespace Colorizer
{
    public class Colorizer
    {
        /**
         * Multimedia Retrieval
         * Heat Kernel Signature project
         * Frank Borgonjen, Joeri van der Lei & Kevin van der Lei
         * 28-10-2011
         * 
         * The Colorizer class is a utility class containing a main method to color a given model for a given time t.
         */
        static void Main(string[] args)
        {
            if (args.Length != 4)
                Console.WriteLine("Colorizer usage: Colorizer modelpath .vectfilepath .evalfilepath time");

            OffReader o = new OffReader(args[0]);
            double t;
            Double.TryParse(args[2], out t);
            double[] hkt = CalculateHKT(args[1], args[2], t);
            //Determin min/max vals
            double min = Double.MaxValue, max = Double.MinValue;
            for (int i = 0; i < hkt.Length; i++)
            {
                if (hkt[i] < min)
                    min = hkt[i];
                else if (hkt[i] > max)
                    max = hkt[i];
            }
            o.colorizeModel(hkt, min, max);
            Console.ReadLine();
        }

        private static double[] CalculateHKT(String file1, String file2, double t)
        {
            List<List<double>> eigenvectors = new List<List<double>>();
            List<double> eigenvalues = new List<double>();

            TextReader reader = new StreamReader(file1);

            string line = reader.ReadLine();
            double value;
            for (int i = 0; line != null; i++)
            {
                string[] split = line.Split(",".ToCharArray());
                if (eigenvectors.Count == 0)
                    for (int j = 0; j < split.Length; j++)
                        eigenvectors.Add(new List<double>());
                for (int j = 0; j < split.Length; j++)
                {
                    value = Double.Parse(split[j].Replace(".", ","), System.Globalization.NumberStyles.Float);
                    eigenvectors[j].Add(value);
                }
                line = reader.ReadLine();
            }

            reader.Close();

            reader = new StreamReader(file2);

            line = reader.ReadLine();
            for (int i = 0; line != null; i++)
            {
                string[] split = line.Split(",".ToCharArray());
                value = Double.Parse(split[i].Replace(".", ","), System.Globalization.NumberStyles.Float);
                eigenvalues.Add(value);
                line = reader.ReadLine();
            }

            reader.Close();

            double[,] eigenvectormatrix = new double[eigenvectors.Count, eigenvectors[0].Count];
            for (int i = 0; i < eigenvectormatrix.GetLength(0); i++)
                for (int j = 0; j < eigenvectormatrix.GetLength(1); j++)
                    eigenvectormatrix[i, j] = eigenvectors[i][j];
            //Calculate values for t
            double[] result = new double[eigenvectormatrix.GetLength(1)];
            for (int x = 0; x < eigenvectormatrix.GetLength(1); x++)
            {
                result[x] = MR_HKSCalc.HKSCalculator.HKT(x, t, eigenvectormatrix, eigenvalues) ;
            }
            return result;
            //double[] res = s.getHKSValues(Path.GetFileNameWithoutExtension(file1));

        }
    }
}
